#include "Com_tcp.h"
#include "Com_format.h"
/**
 * @msg:读取指令
 * @param {int} sockfd 连接套接字
 * @param {char} *COM 读取到的指令
 * @return {*} 成功返回 1 ； 失败返回 -1
 */
int datarecv(int sockfd,char **COM)
{
    char * p = (char *)malloc(13);
    if(NULL == p)
    {
        perror("malloc");
        return -1;
    }

    //读头数据
    int ret = 0;
    ret = read(sockfd,p,13);
    if(-1 == ret)
    {
        perror("read");
        return -1;
    }
    
    int data_size = *(long long *)(p+4);
    //开辟空间存放数据
    char *data = (char *)malloc(data_size);
    ret = read(sockfd,data,data_size);
    if(-1 == ret)
    {
        perror("read");
        return -1;
    }
    //将数据封装好传给COM
    *COM = (char *)malloc(data_size + 13);
    char *q = *COM;

    memcpy((void *)*COM,(void *)p,13);
    *COM += 13;
    memcpy((void*)*COM,(void*)data,data_size);

    *COM = q;

    free(p);
    free(data);
    return 1;
}
